package PagePackage;
import static org.testng.AssertJUnit.assertEquals;

import org.openqa.selenium.By;
import org.openqa.selenium.ElementNotVisibleException;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class schoolsPage extends internalPageClass{
	@FindBy (className="btn-add-new")
	public WebElement addNewSchoolButton;
	
	@FindBy (name="name")
	public WebElement schoolName;	
	
	@FindBy (className="btn-search")
	public WebElement searchButton; 
	
public newTeamPage scrollAndClickTeam( int x, int y, String teamName) {
		
		WebElement newTeamClick=driver.findElement(By.xpath(teamName));
		 
	    String code = "window.scroll(" + (newTeamClick.getLocation().x + x) + ","
	                                 + (newTeamClick.getLocation().y + y) + ");";
	 
	    ((JavascriptExecutor)driver).executeScript(code, newTeamClick, x, y); 
		 WebElement edit2 = newTeamClick.findElement(By.xpath("../td/button[contains(@class,'edit')]"));
		 ((JavascriptExecutor) driver).executeScript("arguments[0].click();", edit2);
		 return MyPageFactory.getPage(driver, newTeamPage.class);
		 
	}
	
public newSchoolPage clickAddNewSchoolButton() {
	addNewSchoolButton.click();
	return MyPageFactory.getPage(driver, newSchoolPage.class);
}

public newSchoolPage scrollAndClickSchool( final int x, final int y, final String schoolName) {
	Boolean until = new WebDriverWait( driver, 30 ).until( new ExpectedCondition<Boolean>()
	        {
	            public Boolean apply( WebDriver webDriver )
	            {
	                try
	                {
	
	WebElement newSchoolClick=driver.findElement(By.xpath(schoolName));
	 
    String code = "window.scroll(" + (newSchoolClick.getLocation().x + x) + ","
                                 + (newSchoolClick.getLocation().y + y) + ");";
 
    ((JavascriptExecutor)driver).executeScript(code, newSchoolClick, x, y); 
	 
	 ((JavascriptExecutor) driver).executeScript("arguments[0].click();", newSchoolClick); }
	                catch( StaleElementReferenceException e )
	                {
	                	System.out.println( "Select failed! Try again..." );
	                    return false;
	                }
	                System.out.println( "School is found!!" );
	                return true;
	
	            }
	        } );
	 return MyPageFactory.getPage(driver, newSchoolPage.class);
	 
}
public addLeaguePage scrollAndClickLeague( final int x, final int y, final String leagueName) {
	
	Boolean until = new WebDriverWait( driver, 30 ).until( new ExpectedCondition<Boolean>()
	        {
	            public Boolean apply( WebDriver webDriver )
	            {
	                try
	                {
	
	WebElement newLeagueClick=driver.findElement(By.xpath(leagueName));
	 
    String code = "window.scroll(" + (newLeagueClick.getLocation().x + x) + ","
                                 + (newLeagueClick.getLocation().y + y) + ");";
 
    ((JavascriptExecutor)driver).executeScript(code, newLeagueClick, x, y); 
    
    
	 
	((JavascriptExecutor) driver).executeScript("arguments[0].click();", newLeagueClick);
	
	                }
	                catch( StaleElementReferenceException e )
	                {
	                	System.out.println( "Select failed! Try again..." );
	                    return false;
	                }
	                System.out.println( "League is found!!" );
	                return true;
	
	            }
	        } );
	
	 return MyPageFactory.getPage(driver, addLeaguePage.class);
    
   
	 
}




public schoolsPage scrollAndClickSchool2(int x, int y, String schoolName) {
	 boolean present;
	    try {
	WebElement newSchoolClick=driver.findElement(By.xpath(schoolName));
	 
 String code = "window.scroll(" + (newSchoolClick.getLocation().x + x) + ","
                             + (newSchoolClick.getLocation().y + y) + ");";

 ((JavascriptExecutor)driver).executeScript(code, newSchoolClick, x, y); 
 
     present = true;
  } catch (NoSuchElementException e) {
     present = false;
  }
	    assertEquals(false, present);
	 return MyPageFactory.getPage(driver, schoolsPage.class);
	 
}

public schoolsPage findSchool(String school){
	schoolName.click();
	schoolName.clear();
	schoolName.sendKeys(school);
	searchButton.click();
	 return MyPageFactory.getPage(driver, schoolsPage.class);
}





	public boolean isOnThisPage(){
		return addNewSchoolButton.isDisplayed();
	}
	@Override
	void tryToOpen() {
	MyPageFactory.getPage(driver, loginPageClass.class).loginAs("victoria@krossover.com", "webmaster");
	MyPageFactory.getPage(driver, userRolePageClass.class).clickTeamTab();
		
	
	
}
}